package cn.dengta.webapp.trade.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.annotation.Nonnull;

import cn.dengta.common.model.TimeInterval;
import cn.dengta.common.web.Page;
import cn.dengta.webapp.base.service.BaseServiceImpl;
import cn.dengta.webapp.trade.dao.InviteIntegralLogDao;
import cn.dengta.webapp.trade.dto.IntegralTradeDTO;
import cn.dengta.webapp.trade.dto.UserIntegralLogSumDTO;
import cn.dengta.webapp.trade.entity.InviteIntegralLog;
import cn.dengta.webapp.trade.form.UserIntegralLogQuery;
import me.codeplayer.util.ArrayUtil;
import me.codeplayer.util.StringUtil;
import org.springframework.stereotype.Service;

/**
 * 邀请奖励日志表 服务实现类
 *
 * @author lgy
 * @since 2022-07-14
 */
@Service
public class InviteIntegralLogService extends BaseServiceImpl<InviteIntegralLog, InviteIntegralLogDao, Long> {

	public boolean log(IntegralTradeDTO dto, BigDecimal fromIntegral, BigDecimal tradeRate) {
		if (dto != null) {
			save(new InviteIntegralLog().init(dto.getFromUserId(), dto.getToUserId(), dto.getCoinTradeType(), dto.getTradeAmount(), fromIntegral, tradeRate, dto.getAssocObj(), dto.getUserCoinLogId(), dto.getRemark(), dto.getNow()));
			return true;
		}
		return false;
	}

	/***
	 * 查询指定用户、指定交易时间段、指定交易类型(可变参数)的推广员交易数据键
	 */
	public List<UserIntegralLogSumDTO> findFrontSumOfInfo(Long[] userIds, TimeInterval addTimeInterval, String groupAttr, String selectKey, boolean userFromOrTo) {
		return baseDao.findSumOfInfo(userIds, addTimeInterval, groupAttr, selectKey, userFromOrTo, true);
	}

	public List<UserIntegralLogSumDTO> findSumOfInfo(Long[] userIds, TimeInterval addTimeInterval, String groupAttr, String selectKey, boolean userFromOrTo) {
		return baseDao.findSumOfInfo(userIds, addTimeInterval, groupAttr, selectKey, userFromOrTo, false);
	}

	public Page<UserIntegralLogSumDTO> findPageSumOfInfo(Page<InviteIntegralLog> page, Long[] userIds, UserIntegralLogQuery query, TimeInterval addTimeInterval, String groupAttr, String selectKey, boolean userFromOrTo) {
		return baseDao.findPageSumOfInfo(page, userIds, query, addTimeInterval, groupAttr, selectKey, userFromOrTo, false);
	}

	/**
	 * 根据用户与时间查询邀请奖励
	 */
	public List<InviteIntegralLog> findInviteIncome(@Nonnull Long[] userIds, @Nonnull TimeInterval addTimeInterval) {
		return baseDao.findInviteIncome(ArrayUtil.getInSQL(null, userIds, true, false).toString(), addTimeInterval.getFinalBegin(), addTimeInterval.getFinalEnd());
	}

	/**
	 * 修改邀请奖励状态
	 */
	public Integer updateInviteIncome(@Nonnull Long userId, @Nonnull Long inviteRewardId, @Nonnull Integer status, String remark) {
		return baseDao.updateInviteIncome(userId, inviteRewardId, status, StringUtil.toString(remark));
	}

	public Integer updateInviteRewardId(@Nonnull Long userId, @Nonnull Long inviteRewardId, @Nonnull Date beginTime, @Nonnull Date endTime) {
		return baseDao.updateInviteRewardId(userId, inviteRewardId, beginTime, endTime);
	}

}
